package cn.com.ejiakanghu.service;

import cn.com.ejiakanghu.core.enums.ResultCode;
import cn.com.ejiakanghu.core.utils.RespUtil;
import cn.com.ejiakanghu.core.vo.BaseResult;
import cn.com.ejiakanghu.dto.RfidDto;
import cn.com.ejiakanghu.form.SoftChainLockForm;
import com.alibaba.fastjson.JSONObject;
import org.linlinjava.litemall.core.util.HttpUtil;
import org.linlinjava.litemall.db.domain.AgedmallLeaseGoodsReturnRecord;
import org.linlinjava.litemall.db.domain.LitemallOrder;
import org.linlinjava.litemall.db.domain.LitemallOrderGoods;
import org.linlinjava.litemall.db.domain.LitemallOrderItem;
import org.linlinjava.litemall.db.service.AgedmallLeaseGoodsReturnRecordService;
import org.linlinjava.litemall.db.service.LitemallOrderGoodsService;
import org.linlinjava.litemall.db.service.LitemallOrderService;
import org.linlinjava.litemall.db.util.OrderUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class SoftChainLockManager {
    private Logger logger = LoggerFactory.getLogger(SoftChainLockManager.class);

    @Value("${litemall.box.getAndBindRfid}")
    private String GETANDBINDRFID;
    @Value("${litemall.box.cancelBindRifd}")
    private String CANCELBINDRIFD;
    @Value("${litemall.box.updateRfidStatus}")
    private String UPDATERFIDSTATUS;

    @Autowired
    private LitemallOrderService litemallOrderService;
    @Autowired
    private AgedmallLeaseGoodsReturnRecordService agedmallLeaseGoodsReturnRecordService;

    public BaseResult lock(SoftChainLockForm softChainLockForm) {
        LitemallOrderItem litemallOrder = litemallOrderService.queryByRfid(softChainLockForm.getRfid());
        if (null != litemallOrder) {
            // 更新订单状态
            litemallOrder.setFinishTime(LocalDateTime.now());
            litemallOrder.setOrderStatus(OrderUtil.STATUS_RETURN_SUCCESS);
            litemallOrderService.updateItem(litemallOrder);
            // 更新归还物品状态
            AgedmallLeaseGoodsReturnRecord agedmallLeaseGoodsReturnRecord = agedmallLeaseGoodsReturnRecordService.findByOrderIdAndSubOrderId(litemallOrder.getParentId()+"", litemallOrder.getId()+"");
            agedmallLeaseGoodsReturnRecord.setReceiveTime(LocalDateTime.now());
            agedmallLeaseGoodsReturnRecord.setEndTime(LocalDateTime.now());
            agedmallLeaseGoodsReturnRecordService.update(agedmallLeaseGoodsReturnRecord);
            // 推广分成
            long diffDays = LocalDate.now().toEpochDay() - litemallOrder.getShipArriveTime().toLocalDate().toEpochDay();
            litemallOrderService.saveSettle(litemallOrder, (int) diffDays);
        }
        return RespUtil.success();
    }

    public RfidDto getAndBindRfid(String storeId) {
        Map<String, String> params = new HashMap<>();
        params.put("storeId", storeId);
        String result = HttpUtil.sendPost(GETANDBINDRFID, params);
        BaseResult<RfidDto> baseResult = JSONObject.parseObject(result, BaseResult.class);
        return baseResult.getData();
    }

    public boolean cancelBindRfid(String rfid) {
        Map<String, String> params = new HashMap<>();
        params.put("rfid", rfid);
        String result = HttpUtil.sendPost(CANCELBINDRIFD, params);
        BaseResult baseResult = JSONObject.parseObject(result, BaseResult.class);
        return ResultCode.SUCCESS.code() == baseResult.getCode();
    }

    public boolean updateRfidStatus(String rfid) {
        Map<String, String> params = new HashMap<>();
        params.put("rfid", rfid);
        String result = HttpUtil.sendPost(UPDATERFIDSTATUS, params);
        BaseResult baseResult = JSONObject.parseObject(result, BaseResult.class);
        return ResultCode.SUCCESS.code() == baseResult.getCode();
    }
}
